package org.xx.b2d;

import java.util.Objects;

import static org.xx.b2d.DeviceUtils.getBestSizeByScreen;

/**
 * 世界的显示参数。
 */
public final class DisplayParams {
    /**
     * 缺省的显示参数。
     */
    public static final DisplayParams DEFAULT = createDefault();

    private static final int MILLISECONDS_PER_SECOND = 1000;

    private final String screenId;
    private final boolean fullScreen;
    private final WorldSize size;
    private final int frameRate;
    private final int frameInterval;

    public DisplayParams(
            String screenId,
            boolean fullScreen,
            WorldSize size,
            int frameRate
    ) {
        this.screenId = screenId != null ? screenId.strip() : "";
        this.fullScreen = fullScreen;
        this.size = Objects.requireNonNull(size);
        if (frameRate < 1 || frameRate > 200) {
            throw new IllegalArgumentException("Illegal frameRate: " + frameRate);
        }
        this.frameRate = frameRate;
        this.frameInterval = MILLISECONDS_PER_SECOND / frameRate;
    }

    private static DisplayParams createDefault() {
        final var screenId = "";

        final var size = getBestSizeByScreen(screenId);
        return new DisplayParams(screenId, true, size, 40);
    }

    public final String getScreenId() {
        return this.screenId;
    }

    public final boolean isFullScreen() {
        return this.fullScreen;
    }

    public final WorldSize getSize() {
        return this.size;
    }

    public final int getFrameInterval() {
        return this.frameInterval;
    }

    @Override
    public int hashCode() {
        return Objects.hash(this.screenId, this.fullScreen, this.size, this.frameRate);
    }

    @Override
    public boolean equals(
            Object obj
    ) {
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof DisplayParams)) {
            return false;
        }
        final var other = (DisplayParams) obj;
        return Objects.equals(this.screenId, other.screenId) && this.fullScreen == other.fullScreen && this.size == other.size
                && this.frameRate == other.frameRate;
    }

    @Override
    public String toString() {
        return "DisplayParams("
                + "screen-id=" + this.screenId + ","
                + "full-screen=" + this.fullScreen + ","
                + "size=" + this.size
                + "frame-rate=" + this.frameRate
                + ")";
    }
}
